package com.wei.duowan.bbs.widget;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.wei.duowan.bbs.App;
import com.wei.duowan.bbs.R;

/**
 * 作者：赵若位
 * 时间：2019/9/19 10:37
 * 联系方式：1070138445
 * 功能：
 */
public class DialogUtils implements View.OnClickListener
{
    private static final String TAG = DialogUtils.class.getSimpleName();

    private Dialog mDialog;
    private Context mContext;
    private TextView mTvTitle, mTvCancel, mTvPositive;
    private OnDialogClickListener mDialogClickListener;

    public DialogUtils(@Nullable Context context)
    {
        if (context == null)
        {
            throw new NullPointerException("The Context of DialogUtils is empty!");
        }
        this.mContext = context;
        init();
    }

    /*设置按钮监听*/
    public DialogUtils setOnDialogClickListener(@Nullable OnDialogClickListener listener)
    {
        this.mDialogClickListener = listener;
        return this;
    }

    /*设置标题*/
    public DialogUtils setTitle(@Nullable String title)
    {
        if (mTvTitle != null)
        {
            mTvTitle.setText(title);
        }
        return this;
    }

    /*设置标题*/
    public DialogUtils setTitle(@StringRes int intRes)
    {
        if (mTvTitle != null)
        {
            String parameter = App.getResource().getString(intRes);
            if (!TextUtils.isEmpty(parameter))
            {
                mTvTitle.setText(parameter);
            }
        }
        return this;
    }

    /*设置按钮文本*/
    public DialogUtils setPositive(@Nullable String positive)
    {
        if (mTvPositive != null)
        {
            mTvPositive.setText(TextUtils.isEmpty(positive) ?
                    App.getResource().getString(R.string.positive) : positive);
        }
        return this;
    }

    /*设置按钮文本*/
    public DialogUtils setCancel(@Nullable String cancel)
    {
        if (mTvCancel != null)
        {
            mTvCancel.setText(TextUtils.isEmpty(cancel) ?
                    App.getResource().getString(R.string.cancel) : cancel);
        }
        return this;
    }


    public DialogUtils setCanceledOnTouchOutside(boolean cancel)
    {
        if (mDialog != null)
        {
            mDialog.setCanceledOnTouchOutside(cancel);
        }
        return this;
    }

    public DialogUtils setCancelable(boolean flag)
    {
        if (mDialog != null)
        {
            mDialog.setCancelable(flag);
        }
        return this;
    }


    /*初始化相关操作*/
    private void init()
    {
        View layout = LayoutInflater.from(mContext).inflate(R.layout.layout_dialog, null);
        mDialog = new Dialog(mContext, R.style.DialogStyle);
        mDialog.setContentView(layout);
        mDialog.setCanceledOnTouchOutside(true);
        mDialog.setCancelable(true);

        //
        mTvTitle = layout.findViewById(R.id.tv_title);
        mTvPositive = layout.findViewById(R.id.tv_positive);
        mTvPositive.setOnClickListener(this);
        mTvCancel = layout.findViewById(R.id.tv_cancel);
        mTvCancel.setOnClickListener(this);
    }


    /*Show*/
    public void show()
    {
        if (mDialog != null && (!mDialog.isShowing()))
        {
            mDialog.show();
        }
    }


    /*Dismiss*/
    public void dismiss()
    {
        if (mDialog != null && mDialog.isShowing())
        {
            mDialog.dismiss();
        }
    }

    @Override
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.tv_positive:
                if (mDialogClickListener != null)
                {
                    mDialogClickListener.setPositiveListener();
                }
                break;
            case R.id.tv_cancel:
                if (mDialogClickListener != null)
                {
                    mDialogClickListener.setCancelLisntener();
                }
                break;
        }
        dismiss();
    }


    public interface OnDialogClickListener
    {
        /*确认监听*/
        void setPositiveListener();

        /*取消监听*/
        void setCancelLisntener();
    }

}
